﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using DAO;
using DTO;
using System.Windows.Forms;

namespace BaiTap1
{
    public partial class frm_QLNguoiDung : Form
    {
        Status TrangThai = Status.view;
        NguoiDung_DTO nguoiDung;
        public frm_QLNguoiDung()
        {
            InitializeComponent();
            LoadData();
            Binding();
        }
        public void DisableButton()
        {
            BT_XOA.Enabled = false;
            BT_THEM.Enabled = false;
            BT_SUA.Enabled = false;

            BT_CAPNHAT.Enabled = true;
            BT_HUY.Enabled = true;
        }
        public void EnableButton()
        {
            BT_XOA.Enabled = true;
            BT_THEM.Enabled = true;
            BT_SUA.Enabled = true;

            BT_CAPNHAT.Enabled = false;
            BT_HUY.Enabled = false;
        }
        public void LoadData()
        {
            NguoiDung_DTObindingSource.DataSource = NguoiDung_DAO.SelectAll();
        }
        public void Binding()
        {
            hoTenTextBox.DataBindings.Clear();
            hoTenTextBox.DataBindings.Add("Text", NguoiDung_DTODataGridView.DataSource, "_Ten_Dang_Nhap");

            TenDangNhapTextBox.DataBindings.Clear();
            TenDangNhapTextBox.DataBindings.Add("Text", NguoiDung_DTODataGridView.DataSource, "_Ten_Dang_Nhap");

            QuyentextBox.DataBindings.Clear();
            QuyentextBox.DataBindings.Add("Text", NguoiDung_DTODataGridView.DataSource, "_Quyen");
        }

        private void BT_THEM_Click(object sender, EventArgs e)
        {
            /*DisableButton();
            TrangThai = Status.insert;
            nguoiDung = NguoiDung_DTObindingSource.AddNew() as NguoiDung_DTO;*/
            frm_TaoTaiKhoan f = new frm_TaoTaiKhoan();
            f.ShowDialog();
        }

        private void BT_SUA_Click(object sender, EventArgs e)
        {
            DisableButton();
            TrangThai = Status.update;
        }

        private void BT_CAPNHAT_Click(object sender, EventArgs e)
        {
            EnableButton();
            if (TrangThai == Status.insert)
            {
                string result = NguoiDung_DAO.Insert(nguoiDung);

                if (result == "true")
                {
                    NguoiDung_DTObindingSource.DataSource = NguoiDung_DAO.SelectAll();
                    nguoiDung = null;
                }
                else
                {
                    MessageBox.Show(result, "Thông tin");
                }
            }
            else if (TrangThai == Status.update)
            {
                string result = NguoiDung_DAO.PhanQuyen(nguoiDung);

                if (result == "true")
                {
                    NguoiDung_DTObindingSource.DataSource = NguoiDung_DAO.SelectAll();
                    nguoiDung = null;
                }
                else
                {
                    MessageBox.Show(result, "Thông tin");
                }
            }
            TrangThai = Status.view;
        }

        private void BT_HUY_Click(object sender, EventArgs e)
        {
            EnableButton();
            TrangThai = Status.view;
        }
    }
}
